package com.xzlf.quartz;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.util.concurrent.TimeUnit;

public class QuartzTest {
    public static void main(String[] args) throws SchedulerException, InterruptedException {
        StdSchedulerFactory stdSchedulerFactory = new StdSchedulerFactory();
        // 从工厂获取一个调度器
        Scheduler scheduler = stdSchedulerFactory.getScheduler();
        // 指明job的名称，所在组的名称，以及绑定job类
        JobDetail build = JobBuilder.newJob(MyJob.class)
                .withIdentity("JobName", "JobGroup")
                .usingJobData("name", "quartz").build();
        // 定义触发条件
        SimpleTrigger trigger = TriggerBuilder.newTrigger()
                .withIdentity("CronTrigger", "CronTriggerGroup")
                .withSchedule(SimpleScheduleBuilder.simpleSchedule()
                        .withIntervalInSeconds(3)
                        .repeatForever())
                .startNow()
                .build();
        // 把作业和触发器注册到任务调度中
        scheduler.scheduleJob(build, trigger);

        // 启动调度
        scheduler.start();
        TimeUnit.SECONDS.sleep(10);
        // 停止调度
        scheduler.shutdown(true);
    }
}
